var gulp = require("gulp");
var fileinclude = require("gulp-file-include") ;
var htmlmin = require("gulp-htmlmin") ;
var babel = require("gulp-babel");
// var uglify = require("gulp-uglify");
var sass = require("gulp-sass");
sass.compiler = require("sass");
var connect = require("gulp-connect");
const GulpClient = require("gulp");
var del = require("del")
var cssmin = require("gulp-cssmin");

gulp.task("connect" , function(done){
    // 把服务器的根目录放在dist文件夹之中
    // 因为很多src之中的代码很多是不可查看的
    connect.server({
        // 根目录配置
        root : "./dist",
        // 设置端口  不要把端口号设置成两位数的端口号
        port : 3000 ,
        // 自动刷新功能的调用
        livereload : true
    });
    done();
})

gulp.task("html" , function( done ) {
    gulp.src(["./src/html/*.html"])
        .pipe(fileinclude({
            prefix : "@@" ,
            basepath : "@file"
        }))
        .pipe(gulp.dest("./dist/html"))
        // 自动刷新功能调用
        .pipe(connect.reload());
        done() ;
})

gulp.task("html-min" , function( done ) {
    gulp.src(["./src/html/*.html"])
        .pipe(fileinclude({
            prefix : "@@" ,
            basepath : "@file"
        }))
        .pipe(htmlmin({collapseWhitespace : true}))
        .pipe(gulp.dest("./dist/html"))
        done() ;
})

gulp.task("javascript" , function( done ){
    gulp.src(["./src/js/*.js"])
        .pipe(babel({
            presets:['@babel/env']
        }))
        .pipe(gulp.dest("./dist/js"))
        .pipe(connect.reload());
    done();
})


// gulp.task("javascript-uglify" , function( done){
//     gulp.src(["./src/js/*.js"])
//         .pipe(babel({
//             preset:['@babel/env']
//         }))
//         .pipe(uglify())
//         .pipe(gulp.dest("./dist/js"))
//     done();
// })

gulp.task("scss" , function(done){
    gulp.src(["./src/scss/*.scss"])
        .pipe(sass().on("error" , sass.logError))
        .pipe(gulp.dest("./dist/css"))
        .pipe(connect.reload())
    done();
})

gulp.task("cssmin" , function(done){
    gulp.src(["./src/scss/*.scss"])
        .pipe(sass().on("error" , sass.logError))
        .pipe(cssmin())
        .pipe(gulp.dest("./dist/css"))
        .pipe(connect.reload())
    done();
})

gulp.task("libs" , function( done ){
    gulp.src(["./src/iconfont/font-awesome-4.7.0/*/**/*"])
        .pipe(gulp.dest("./dist/iconfont/font-awesome-4.7.0/"));
    gulp.src([ "./src/img/**/*"])
        .pipe(gulp.dest("./dist/img"))
    gulp.src(["./src/js/libs/**/*"])
        .pipe(gulp.dest("./dist/js/libs"))
    gulp.src(["./src/json/**/*"])
        .pipe(gulp.dest("./dist/json"))
        done()
})

gulp.task("del" , function(done){
    del("./dist/*/**/*");
    done();
})

gulp.task("watch" , function( done ){

    gulp.watch(["./src/html/*.html","./src/components/*.html"] , gulp.series("html"));
    gulp.watch(["./src/js/*.js"] , gulp.series("javascript"));
    gulp.watch(["./src/scss/*.scss"] , gulp.series("scss"));
    gulp.watch(["./src/iconfont/**/*" , "./src/img/**/*" , "./src/js/libs/*.js"] , gulp.series("libs"));
 
    done();
});

var dev_task_list = gulp.series("del","html", "javascript","scss","libs",gulp.parallel("watch" , "connect"));

gulp.task("default",dev_task_list);

// var build_task_list = gulp.series("del" , "html-min", "javascript-uglify","cssmin","libs");

// gulp.task("build",build_task_list);

var build_task_list = gulp.series("del" , "html-min","cssmin","libs");

gulp.task("build",build_task_list);